<template>
    <el-aside width="auto" height="auto">
        <el-menu :collapse="isCollapse" :default-active="onRoutes" background-color="#232323" text-color="#ccc" active-text-color="#ddd" unique-opened router>
            <template v-for="item in items">
                <template v-if="item.subItems">
                    <el-submenu :index="item.path" :key="item.path">
                        <template slot="title">
                            <i :class="item.icon"></i>
                            <span slot="title">{{item.title}}</span>
                        </template>
                        <el-menu-item v-for="(subItem, i) in item.subItems" :index="subItem.path" :key="i">
                            {{subItem.title}}
                        </el-menu-item>
                    </el-submenu>
                </template>
                <template v-else>
                    <el-menu-item :index="item.path" :key="item.path">
                        <i :class="item.icon"></i>
                        <span slot="title">{{item.title}}</span>
                    </el-menu-item>
                </template>
            </template>
        </el-menu>
    </el-aside>
</template>

<script>
import Bus from "./bus";
export default {
    name: "Sidebar",
    data() {
        return {
            isCollapse: false,
            items: [
                {
                    title: "系统首页",
                    path: "/index",
                    icon: "el-icon-setting",
                },
                {
                    title: "业主信息管理",
                    path: "/owner",
                    icon: "el-icon-star-on",
                },
                {
                    title: "信息通报管理",
                    path: "/notice",
                    icon: "el-icon-star-on",
                },
                {
                    title: "服务反馈管理",
                    path: "/service",
                    icon: "el-icon-star-on",
                },
                {
                    title: "报修管理",
                    path: "/repair",
                    icon: "el-icon-star-on",
                },
                {
                    title: "小区信息管理",
                    path: "/information",
                    icon: "el-icon-star-on",
                },
                {
                    title: "停车管理",
                    path: "/car",
                    icon: "el-icon-star-on",
                },
                {
                    title: "房屋管理",
                    path: "/house",
                    icon: "el-icon-star-on",
                },
                {
                    title: "管理员管理",
                    path: "/admin",
                    icon: "el-icon-star-on",
                },
            ],
        };
    },
    computed: {
        onRoutes() {
            return this.$route.fullPath;
        },
    },
    created() {
        // 通过 event bus进行组件间的通信 来折叠和展开侧边栏
        Bus.$on("collapse", (isCollapse) => {
            this.isCollapse = isCollapse;
        });
    },
};
</script>

<style scoped lang="less">
.el-menu {
    height: 100%;
    border: none;
    &:not(.el-menu--collapse) {
        width: 200px;
    }
}
.el-menu-item.is-active {
    border-left: 3px solid @mainColor;
    background-color: #171717 !important;
}
</style>
